﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public partial class Team
    {
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddNew(ref Team team)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                string teamName = team.Name;

                if (context.Teams.Any(a => a.Name == teamName))
                {
                    team = null;
                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.DUPPLICATE;
                }
                else
                {
                    Team t = new Team();
                    t.Name = team.Name;
                    t.Leader = 0;
                    t.CreatedBy = team.CreatedBy;
                    t.CreatedDateTime = DateTime.Now;
                    t.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                    t.RowVersion = 1;

                    context.Teams.InsertOnSubmit(t);

                    context.SubmitChanges();

                    team = t;

                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
                }
            }
            catch
            {
                team = null;
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetTeamList(ref List<Team> listTeam)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                var query = context.Teams;

                listTeam = query.ToList();

                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;

            }
            catch
            {
                listTeam = null;
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }
    }
}
